//网站访问部分

const fs = require('fs');
const path = require('path');

const articleSchema = require('../../schemas/articleSchema.js');//文章表
const typeSchema = require('../../schemas/typeSchema.js');//分类表
const tagSchema = require('../../schemas/tagSchema.js');//tag标签表
const messageSchema = require('../../schemas/messageSchema.js');//message表

//按页获取文章
exports.getArticleByPage = async ctx=>{
	let {page,num} = ctx.query;
	num = num - 0;
	page = page -0;
	let totals = await articleSchema.find({}).count();
	let articles = await articleSchema.find({}).sort({time:-1}).skip((page - 1 ) * num).limit(num);
	let articleList = [];
	for(let i = 0;i < articles.length;i++){
		let types = await typeSchema.find({articleId:articles[i].id});
		let tags = await tagSchema.find({articleId:articles[i].id});
		articleList.push({
			id:articles[i].id,
			title:articles[i].title,
			description:articles[i].description,
			keywords:articles[i].keywords,
			imgAlt:articles[i].imgAlt,
			imgTitle:articles[i].imgTitle,
			content:articles[i].textContent,
			time:articles[i].time,
			views:articles[i].views,
			messages:articles[i].messages,
			imgUrl:articles[i].imgUrl,
			type:types[0].typeName,
			tags
		})
	}
	ctx.body = {
		data:{
			articleList,
			totals
		}
	}
};

//根据tag获取文章
exports.getArticleByTag = async ctx=>{
	let {tagId,page,num} = ctx.query;
	num= num - 0;
	page = page - 0;
	let totals = await tagSchema.find({tagId}).count();
	//根据tag获取文章ID
	let articleIds = await tagSchema.find({tagId}).sort({time:-1}).skip((page - 1) * num).limit(num);
	//根据文章ID获取文章
	let articleList = [];
	for(let i = 0;i < articleIds.length;i++){
		let result = await articleSchema.findOne({id:articleIds[i].articleId});
		let types = await typeSchema.find({articleId:articleIds[i].articleId});
		let tags = await tagSchema.find({articleId:articleIds[i].articleId});
		articleList.push({
			id:articleIds[i].articleId,
			title:result.title,
			description:result.description,
			keywords:result.keywords,
			imgAlt:result.imgAlt,
			imgTitle:result.imgTitle,
			content:result.textContent,
			time:result.time,
			views:result.views,
			messages:result.messages,
			imgUrl:result.imgUrl,
			type:types[0].typeName,
			tags
		})
	}
	ctx.body = {
		data:{
			articleList,
			totals
		}
	}
};
//根据分类获取文章
exports.getWebArticleByType = async ctx=>{
	let {typeId,page,num} = ctx.query;
	let typeList = GetWebTypes();
	let typeName = typeList.find(item=>{
		return item.id - 0 === typeId - 0;
	}).name;
	num= num - 0;
	page = page - 0;
	let totals = await typeSchema.find({typeId}).count();
	//根据分类获取文章ID
	let articleIds = await typeSchema.find({typeId}).sort({time:-1}).skip((page - 1) * num).limit(num);
	//根据文章ID获取文章
	let articleList = [];
	for(let i = 0;i < articleIds.length;i++){
		let result = await articleSchema.findOne({id:articleIds[i].articleId});
		let types = await typeSchema.find({articleId:articleIds[i].articleId});
		let tags = await tagSchema.find({articleId:articleIds[i].articleId});
		articleList.push({
			id:articleIds[i].articleId,
			title:result.title,
			description:result.description,
			keywords:result.keywords,
			imgAlt:result.imgAlt,
			imgTitle:result.imgTitle,
			content:result.textContent,
			time:result.time,
			views:result.views,
			messages:result.messages,
			imgUrl:result.imgUrl,
			type:typeName,
			tags
		})
	}
	ctx.body = {
		data:{
			articleList,
			totals
		}
	}
};
//获取阅读最多的文章
exports.getWebPageByViews = async ctx=>{
	let {num} = ctx.query;
	num = num - 0;
	let result = await articleSchema.find({}).sort({views:-1}).limit(num);
	let data = result.map(item=>{
		return {
			name:item.title,
			id:item.id
		}
	});
	ctx.body = {
		data
	}
};
//获取最新发布的文章
exports.getWebPageByTime = async ctx=>{
	let {num} = ctx.query;
	num = num - 0;
	let result = await articleSchema.find({}).sort({time:-1}).limit(num);
	let data = result.map(item=>{
		return {
			name:item.title,
			id:item.id,
			title:item.title,
			content:item.textContent,
			imgUrl:item.imgUrl,
			imgAlt:item.imgAlt,
			imgTitle:item.imgTitle
		}
	});
	ctx.body = {
		data
	}
};

//根据文章ID获取文章  阅读具体文章
exports.getWebArticleById = async ctx=>{
	let {id} = ctx.query;
	let result = await articleSchema.findOne({id});
	if(result){
		let {title,time,views,description,keywords,messages,textContent,htmlContent,imgUrl,imgAlt,imgTitle} = result;
		let types = await typeSchema.find({articleId:id});
		let tags = await tagSchema.find({articleId: id});
		await articleSchema.updateOne({id},{$set:{views:views + 1}});
		let relatives = await typeSchema.find({typeId: types[0].typeId}).sort({time:-1}).limit(8);
		let data = {
			title,
			description,
			keywords,
			time,
			views,
			messages,
			textContent,
			htmlContent,
			tags,
			types,
			imgUrl,
			imgAlt,
			imgTitle,
			relatives
		};
		ctx.body = {
			data
		}
	}else{
		ctx.body = {
			data:null
		}
	}
};

//根据文章ID获取用户评论
exports.getArticleMessage = async ctx=>{
	let {articleId} = ctx.query;
	let result = await messageSchema.find({articleId}).sort({time:-1});
	ctx.body = {
		data:result
	}
};
//添加文章评论
exports.publishArticleMessage = async ctx=>{
	let {articleId,authorName,time,content} = ctx.request.body;
	await messageSchema.create({
		articleId,
		authorName,
		time,
		content
	});
	let {messages} = await articleSchema.findOne({id:articleId});
	messages += 1;
	await articleSchema.updateOne({id:articleId},{$set:{messages}});
	ctx.body = {
		status:true
	}
};

//查找文章
exports.searchArticle = async ctx=>{
	let {searchValue} = ctx.request.body;
	let result = await articleSchema.find({title:new RegExp(searchValue,'i')});
	ctx.body = {
		data:result
	}
};

//获取所有文章生成站点地图
exports.getAllArticle = async ctx=>{
	let result = await articleSchema.find();
	let data = result.map(item=>{
		return item.id
	});
	ctx.body = {
		data
	}
};
//获取所有type分页
exports.getAllTypePage = async ctx=>{
	let types = GetWebTypes();
	let len = types.length;
	let data = [];
	for(let i = 0;i < len;i++){
		let num = await typeSchema.find({typeId:types[i].id}).count();
		data.push({
			id:types[i].id,
			num
		})
	}
	ctx.body = {
		data
	}
};
//获取所有tag分页
exports.getAllTagPage = async ctx=>{
	let tags = GetWebTags();
	let len = tags.length;
	let data = [];
	for(let i = 0;i < len;i++){
		let num = await tagSchema.find({tagId:tags[i].id}).count();
		data.push({
			id:tags[i].id,
			num
		})
	}
	ctx.body = {
		data
	}
};



function GetWebTypes(){
	let result = fs.readFileSync('./data/requiredJSON/webTypes.json');
	let types = JSON.parse(result.toString());
	return types;
}
function GetWebTags(){
	let result = fs.readFileSync('./data/requiredJSON/webTags.json');
	let tags = JSON.parse(result.toString());
	return tags;
}
